﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MyDataService;
using QuanLyBanHang.DAL;
using QuanLyBanHang.OBJECT;

namespace QuanLyBanHang
{
    public partial class frmBanHang : Form
    {
        private DalBanHang banHang = new DalBanHang();
        private List<ObjHangHoa> list = new List<ObjHangHoa>();

        public frmBanHang()
        {
            InitializeComponent();
            dataservice.strConn = ConfigurationManager.ConnectionStrings["strConn"].ConnectionString;
            this.Text = "Bán Hàng - Quầy1 - " + SessionPrivate.NhanVien.MaNV;
        }

        private ObjHangHoa objHangHoa;

        private void btnGenRandom_Click(object sender, EventArgs e)
        {
            if (updateSoluong == true)
            {
                dgvListSanPham.SelectedRows[0].Cells[3].Value = numericUpDownSoLuong.Value;
                updateSoluong = false;
                numericUpDownSoLuong.Value = 1;
            }
            else
            {
            InsertOld();
            objHangHoa = new ObjHangHoa();
            objHangHoa = banHang.genRandom();
            if (!banHang.exHanSuDung(objHangHoa.Ma))
            {
                MessageBox.Show("Sản phẩm " + objHangHoa.Ten + " có mã " + objHangHoa.Ma +
                                " đã hết hạn sử dụng. \r\n Không thể thêm được sản phẩm này");
                txtMaVach.Text = "";
                lblTenSanPham.Text = "";
                lblGiaBan.Text = "";
                StateButton();
                return;
            }
            txtMaVach.Text = objHangHoa.Ma;
            lblTenSanPham.Text = objHangHoa.Ten;
            lblGiaBan.Text = objHangHoa.Giaban.ToString();
            }
            UpdateTotalMoney();
            StateButton();
        }
        void UpdateSoLuong()
        {
            if (updateSoluong == true)
            {
                dgvListSanPham.SelectedRows[0].Cells[3].Value = numericUpDownSoLuong.Value;
            }
        }

        public void InsertOld()
        {
            if (!string.IsNullOrEmpty(txtMaVach.Text))
            {
                bool Ex = true;
                for (int i = 0; i < dgvListSanPham.Rows.Count; i++)
                {
                    if (dgvListSanPham.Rows[i].Cells[0].Value.ToString()==txtMaVach.Text)
                    {
                        int slHientai = int.Parse(dgvListSanPham.Rows[i].Cells[3].Value.ToString());
                        slHientai += int.Parse(numericUpDownSoLuong.Value.ToString());
                        dgvListSanPham.Rows[i].Cells[3].Value = slHientai;
                        Ex = false;
                    break;
                    
                }
                }
                if (Ex)
                {
                     dgvListSanPham.Rows.Add(new string[]
                    {txtMaVach.Text, lblTenSanPham.Text, lblGiaBan.Text, numericUpDownSoLuong.Value.ToString()});
                    list.Add(objHangHoa);
                }
               
            }
        }

        public void UpdateTotalMoney()
        {
            double TotalMoney = 0.0;
            for (int i = 0; i < dgvListSanPham.Rows.Count; i++)
            {
                try
                {
                    TotalMoney += Convert.ToDouble(dgvListSanPham.Rows[i].Cells[2].Value.ToString())*
                                  Convert.ToDouble(dgvListSanPham.Rows[i].Cells[3].Value.ToString());
                }
                catch (Exception)
                {
                    continue;
                }

            }
            lblTongCong.Text = TotalMoney.ToString() + " VND";
        }

        public void StateButton()
        {
            if (list.Count == 0&txtMaVach.Text=="")
            {

                btnXoa.Enabled = false;
                btnThanhToan.Enabled = false;
                btnHuyPhien.Enabled = false;
            }
            else
            {
                btnThanhToan.Enabled = true;
                btnHuyPhien.Enabled = true;

                if (dgvListSanPham.SelectedRows.Count == 1)
                {

                    btnXoa.Enabled = true;

                   
                }
                if (dgvListSanPham.SelectedRows.Count > 1)
                {

                }
            }

        }

        private void btnThanhToan_Click(object sender, EventArgs e)
        {
            InsertOld();
            UpdateTotalMoney();
            double TongCong = int.Parse(lblTongCong.Text.Substring(0, lblTongCong.Text.Length - 4));
            double TienMat = 0.0;
            if (txtTienMat.Text == "")
            {
                MessageBox.Show("Chưa nhập tiền thanh toán");
                return;
            }
            else
            {
               TienMat = int.Parse(txtTienMat.Text);
            }

            if (TienMat<TongCong)
            {
                MessageBox.Show("Thiếu tiền . ");return;
            }
            lblTienThoiLai.Text = (TienMat - TongCong).ToString()+" VND";
            banHang.BanHangHoa(dgvListSanPham);

            btnHuyPhien_Click(sender,e);
           
        }

        public void XoaSelect()
        {
            if (dgvListSanPham.SelectedRows.Count==0)
            {
                return;
            }
            else
            {
                if (dgvListSanPham.SelectedRows.Count==1)
                {

                    if (MessageBox.Show("Bạn có muốn xóa " + dgvListSanPham.SelectedRows[0].Cells[1].Value.ToString() + ".", "Xóa", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        dgvListSanPham.Rows.RemoveAt(dgvListSanPham.SelectedRows[0].Index);
                    }
                }
                if (dgvListSanPham.SelectedRows.Count>1)
                {
                    if (MessageBox.Show("Bạn có muốn xóa sản phẩm đã chọn","Xóa",MessageBoxButtons.YesNo)==DialogResult.Yes)
                    {
                        foreach (DataGridViewRow row in dgvListSanPham.SelectedRows )
                        {
                            dgvListSanPham.Rows.Remove(row);
                        }
                    }
                }
            }
            UpdateTotalMoney();
        }

        private void btnHuyPhien_Click(object sender, EventArgs e)
        {
            lblGiaBan.Text = "0 VND";
            lblTenSanPham.Text = "Chưa chọn sản phẩm";
            txtMaVach.Text = "";
            lblTienThoiLai.Text = "0 VND";
            lblTongCong.Text = "0 VND";
            txtTienMat.Text = "0";
            numericUpDownSoLuong.Value = 1;
            dgvListSanPham.Rows.Clear();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            XoaSelect();
        }

        private void dgvListSanPham_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode==Keys.Delete)
            {
                XoaSelect();
            }
        }

        private void txtMaVach_TextChanged(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtMaVach.Text))
            {
                return;
            }
            if (banHang.exMaVach(txtMaVach.Text)==0)
            {
                cboFindHangHoa.Visible = true;
            }
            else
            {
                cboFindHangHoa.Visible = false;
            }
        }

        private void cboFindHangHoa_VisibleChanged(object sender, EventArgs e)
        {
            if (cboFindHangHoa.Visible)
            {
                picCheckHangHoa.Image = Properties.Resources.cancel;
            }
            else
            {
                picCheckHangHoa.Image = Properties.Resources.success;
                if (!banHang.exHanSuDung(txtMaVach.Text))
                {
                    MessageBox.Show("Sản phẩm đã hết hạn sử dụng. \r\n Không thể thêm được sản phẩm này");
                    txtMaVach.Text = "";
                    lblTenSanPham.Text = "";
                    lblGiaBan.Text = "";
                    return;
                }
            }
        }

        private void cboFindHangHoa_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (isFormLoad==true)
            {
                return;
            }
            txtMaVach.Text = cboFindHangHoa.SelectedValue.ToString();
        }

        bool isFormLoad=true;
        private void frmBanHang_Load(object sender, EventArgs e)
        {
            banHang.loadcboHangHoa(cboFindHangHoa);
            isFormLoad = false;
        }

        private void btnXoaVuaNhap_Click(object sender, EventArgs e)
        {
            dgvListSanPham.Rows.RemoveAt(dgvListSanPham.Rows.Count-1);
        }

        private void dgvListSanPham_MouseClick(object sender, MouseEventArgs e)
        {
            
           
        }

        private void contextMenuStripGrid_Opening(object sender, CancelEventArgs e)
        {
            if(dgvListSanPham.SelectedRows.Count == 0)
            {
                contextMenuStripGrid.Enabled = false;
            }
        }

        private void xóaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            XoaSelect();
        }

        private void picDanhDau_Click(object sender, EventArgs e)
        {
            if (picDanhDau.Tag.ToString() == "UnChecked")
            {
            if (txtMaVach.Text=="")
            {
                return;
            }
            else
            {
                TextBox txtComment=new TextBox();
                txtComment.Name = "txtComment";
                txtComment.Text = "Nhập nội dung đánh dấu";
                txtComment.Width = 580;
                txtComment.BringToFront();
                txtComment.Location=new Point(36,158);
                txtComment.KeyDown += txtComment_KeyDown;
                this.groupBoxThongTin.Controls.Add(txtComment);
                
            }
                picDanhDau.Image = Properties.Resources.import;
                lblDanhDau.Visible = false;
                picDanhDau.Tag = "Checked";

            }
            else
            {
                TextBox txtComment = this.groupBoxThongTin.Controls.Find("txtComment", true).FirstOrDefault() as TextBox;
                if (txtMaVach.Text != "" & txtComment.Text != "")
                {
                    banHang.DanhDau(txtMaVach.Text, txtComment.Text);
                    MessageBox.Show("Đã đánh dấu sản phẩn này");
                }
                this.groupBoxThongTin.Controls.Remove(txtComment);
                picDanhDau.Image = Properties.Resources.flag;
                lblDanhDau.Visible = true;
                picDanhDau.Tag = "UnChecked";
            }
        }

        
        void txtComment_KeyDown(object sender, KeyEventArgs e)
        {
            TextBox txtComment = (TextBox) sender;
            if (e.KeyCode==Keys.Enter&txtMaVach.Text!=""&txtComment.Text!="")
            {
                banHang.DanhDau(txtMaVach.Text, txtComment.Text);
                MessageBox.Show("Đã đánh dấu sản phẩn này");
                this.groupBoxThongTin.Controls.Remove(txtComment);
            }
           
        }

        private bool updateSoluong = false;
        private void dgvListSanPham_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            
            
        }

        private void dgvListSanPham_Click(object sender, EventArgs e)
        {
if (dgvListSanPham.SelectedRows.Count==1)
            {
                txtMaVach.Text = dgvListSanPham.SelectedRows[0].Cells[0].Value.ToString();
                lblTenSanPham.Text = dgvListSanPham.SelectedRows[0].Cells[1].Value.ToString();
                lblGiaBan.Text = dgvListSanPham.SelectedRows[0].Cells[2].Value.ToString();
                numericUpDownSoLuong.Value = int.Parse(dgvListSanPham.SelectedRows[0].Cells[3].Value.ToString());
                updateSoluong = true;
            }
        }

       

      

    }
}
